<template>
  <div class="login">
    <van-nav-bar :title="$route.meta.title" />
    <div class="content">
      <div class="loginInfo">
        <!-- <form> -->
        <p>
          <i class="iconfont icon-shoujihao"></i>
          <input
            type="text"
            placeholder="请输入手机号"
            v-model="loginForm.username"
          >
        </p>
        <p>
          <i class="iconfont icon-mima"></i>
          <input
            type="password"
            placeholder="请输入密码"
            v-model="loginForm.password"
          >
        </p>
        <p class="btn">
          <van-button
            @click="login(loginForm)"
            type="primary"
            block
            color="#0079FE"
          >登录</van-button>
        </p>
        <p class="tips">
          <span>忘记密码？</span>
          <span @click="goReg">还没有帐号？去注册</span>
        </p>
        <!-- </form> -->
      </div>
    </div>
  </div>
</template>
<script>
import { mapMutations } from 'vuex'
import { Toast } from 'vant'
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    ...mapMutations(['changeLogin']),
    async login(loginForm) {
      const _this = this
      const reg = /^1[35789]\d{9}$/
      if (this.loginForm.username === '' || this.loginForm.password === '') {
        Toast('用户名或密码不能为空')
      } else if (!reg.test(this.loginForm.username)) {
        Toast('手机号不合法')
      } else {
        const res = await _this.$http.postuser(loginForm)
        console.log(res.data)
        _this.userToken = 'Bearer ' + res.data.id
        // 将用户token保存到vuex中
        _this.changeLogin({ Authorization: _this.userToken })
        _this.$router.push('/home')
        Toast('登录成功')
      }
    },
    goReg() {
      this.$router.push('/register')
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 90%;
  .content {
    width: 100%;
    height: 100%;
    background: linear-gradient(#199ed8 30%, #fff 10%);
    position: relative;
    .loginInfo {
      position: absolute;
      left: 50%;
      top: 20%;
      box-shadow: -2px -2px 2px #888888;
      border-radius: 4px 4px 0 0;
      transform: translateX(-50%);
      width: 80%;
      background-color: #fff;
      border: 0.5px solid #efefef;
      p {
        width: 100%;
        height: 15px;
        margin-top: 15px;
        display: flex;
        justify-content: start;
        align-items: center;
        border-bottom: 1px solid #eee;
        i {
          font-size: 10px;
          top: 5px;
        }
        input {
          text-indent: 4px;
          height: 100%;
          border: none;
        }
      }
      .btn {
        width: 80%;
        margin: 15px auto;
      }
      .tips {
        padding: 0 10px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
</style>
